<?php
namespace Home\Controller;

class WidgetController extends BaseController {


    //
    private $widget;

    /*
     * 各属性对应的名称
     */
    private $property = array(

    );
    
    
    /*
     * 构造
     */
    public function __construct()
    {
        parent::__construct();
        $this -> widget = M('map');
    }


    /**
     * @Function    查询
     * @description 构件属性的查询
     * @Adres      /index.php/Home/Widget/find.html
     * @DateTime    2018-03-13
     * @Author      fly
     * @param       [type]
     * @return      [json]
     *    {"info":
     *      {"road_name":"11234\u7389\u51fd\u8def\u8857\u9053","road_legth":"1234KM",
     *       "road_admin":"1234\u8def\u6865\u96c6\u56e2","road_design":"1234\u540c\u610f\u96c6\u56e2",
     *       "road_construction":"\u5c71\u4e1c\u9ad8\u901f\u96c6\u56e2"},
     *    "status":true,"error":""}
     */
    public function find($param=null){  //$this->ceshi();
        $where = array(
            'mapname' => I('post.name'),
            'mapid' => I('post.id'),
        );
        $result = $this -> widget -> where($where) -> find();
        if(!$result)
            $this -> ferror('不存在');

        if($param===null)
            $this -> fsucces(json_decode($result['val']));
    }


    /**
     * @Function    添加
     * @description 构件属性的添加
     * @Adres       /index.php/Home/Widget/insert.html
     * @Author      fly
     * @DateTime    2018-03-13
     * @return      [json]  {"info":"ok","status":true,"error":""}
     */
    public function insert(){// $this->ceshi();
        //判断用户级别显示
        $this->userauth('json');
        //
        if( !I('post.name') || !I('post.id') )
            $this -> ferror('参数错误');

        //init
        $save = array(
            'mapname' => I('post.name'),
            'mapid' => I('post.id'),
            'lasttime' => time()
        );
        unset($_POST['name']);
        unset($_POST['id']);

        //首先匹配上传的参数中是否有空值
        foreach($_POST as $k => $v){
            if(!empty($k) && !empty($v))
                $result[$k] = $v;
        }
        $save['val'] = $result ? json_encode($result) : "";
        
        //信息覆盖
        $where = array(
            'mapname' => $save['mapname'] ,
            'mapid' => $save['mapid']
        );
        $infotmp = $this -> widget -> where($where) -> find();
        if($infotmp)
            $result = $this -> widget -> where($where) -> save($save);
        else
            $result = $this -> widget -> add($save);

        //
        if($result) $this -> fsucces('ok');
        else $this -> ferror('未知错误');
    }


    /**
     * @Function    修改
     * @description 构件属性的修改
     * @Adres       /index.php/Home/Widget/update.html
     * @Author      fly
     * @DateTime    2018-03-13
     * @return      [json]  {"info":"ok","status":true,"error":""}
     */
    public function update(){
        //判断用户级别显示
        $this->userauth('json');
        //init
        $where = array(
            'mapname' => I('post.name'),
            'mapid' => I('post.id'),
        );
        unset($_POST['name']);
        unset($_POST['id']);

        $result = $this -> widget -> where($where) -> find();
        if(!$result)
            $this -> ferror('图层下ID不存在');

        //
        $info = json_decode($result['val'],true);
        foreach($info as $k => $v){
            if($_POST[$k])
                $info[$k] = I('post.'.$k);
            else
                $this -> ferror('参数上传错误');
        }

        //save
        $save = array('val' => json_encode($info));
        $result = $this -> widget -> where($where) -> save($save);
        if($result) $this -> fsucces('ok');
        else $this -> ferror('没有修改的数据');

    }


    /**
     * @Function    删除
     * @description 构件属性的删除
     * @Adres       /index.php/Home/Widget/delete.html
     * @Author      fly
     * @DateTime    2018-03-13
     * @return      [json]  {"info":"ok","status":true,"error":""}
     */
    public function delete(){
        //判断用户级别显示
        $this->userauth('json');
        //
        //init
        $where = array(
            'mapname' => I('post.name'),
            'mapid' => I('post.id'),
        );
        //
        $result = $this -> widget -> where($where) -> find();
        if(!$result)
            $this -> ferror('图层下ID不存在');
        //
        $result = $this -> widget -> where($where) -> delete();
        if($result) $this -> fsucces('ok');
        else $this -> ferror('删除失败');
    }



    /*
    * 数据测试使用
    */
    public function ceshi(){
        $_POST['name'] = "001";
        $_POST['id'] = 001;
        $_POST['road_name'] = "11234玉函路街道";
        $_POST['road_legth'] = "1234KM";
        $_POST['road_admin'] = "1234路桥集团";
        $_POST['road_design'] = "1234同意集团";
        $_POST['road_construction'] = "山东高速集团";
    }
    
    
    
    /*
     * 接口
     * 模拟图层列表
     * id : 虚拟图层的id
     *       可传， 显示该图层ID下的全部信息（图层+ID）
     *       可不传， 显示全部虚拟图层
     */
    public function layerlist(){
        $id = I('post.id');
        $where = array('status'=>1);
        if($id){
            $where['layerid'] = $id;
            $info = M('layerinfo') -> field('mapname') -> where($where) -> order('id asc') -> select();
            $result = $info;
        }else{
            $info = M('layer') -> field('id,layername,upid,ttime,defaultshow') -> order('`order` asc,id asc') -> select();
            $result = flytreeArray($info);
        }
        if($info){
            $this -> fsucces($result);
        }else
            $this -> ferror("没有数据");
    }






    /*
     * 接口
     * 获取所有可以显示的真实图层列表
     */
    public function LayerListDefaultShow(){
        $info = M('layer') -> where(array('defaultshow'=>1)) -> order('id asc') -> select(); 
        foreach($info as $v){
            $lists = M('layerinfo') -> field('mapname') 
                     -> where(array('layerid'=>$v['id'],'status'=>1)) -> order('id asc') -> select();
            if($lists){
                $resultTmp = "";
                foreach($lists as $listinfo){
                    $result[] = $listinfo;
                }   
            }
        }
        if($result){
            $this -> fsucces($result);
        }else
            $this -> ferror("没有数据");
    }




    /*
     * 接口
     * 添加模拟图层
     */
    public function layeradd(){
        //判断用户级别显示
        $this->userauth('json');
        //
        if(I('post.name')) $save['mapname'] = I('post.name');
        if(I('post.mapid')) $save['mapid'] = I('post.mapid');
        if(I('post.layer')) $save['layername'] = I('post.layer');
        if(!$save) $this -> ferror("没有上传的数据");
        $save['ttime'] = time();
        $save['status'] = 1;
        if(I("post.id"))
            M('layer') -> where(array('id'=>I('post.id'))) -> save($save);
        else
            $info = M('layer') -> add($save);
        $this -> fsucces('ok');
    }


    /*
     * 接口
     * 删除模拟图层
     */
    public function layerdel(){
        //判断用户级别显示
        $this->userauth('json');
        //
        if(I('post.layer'))  $save['layername'] = I('post.layer');
        if(I('post.layerid'))  $save['id'] = I('post.layerid');
        if(!$save) $this -> ferror("参数错误");

        M('layer') -> where($save) -> delete();
        $this -> fsucces('ok');
    }



    /*
     * 修改insert数据
     *   http://localhost/tianjin/index.php/Home/Widget/fly.html
     */
    public function fly(){
        $map = M('map');
        $info = $map -> where(array('type'=>1)) -> select();

        foreach($info as $v){
            //将所有数据转为 json 并存入.
            $infotmp = $this->insertchange($v['val']);

            $save['val'] = $infotmp;
            $save['mapname'] = $v['mapname'];
            $save['mapid'] = $v['mapid'];
            $save['lasttime'] = time();
            $save['type'] = 2;
            $map -> where(array('id'=>$v['id'])) -> save($save);
        }
    }



    /*
     *
     */

}
